package com.hqyj.springboot.modules.test.service.impl;

import com.hqyj.springboot.modules.common.vo.Result;
import com.hqyj.springboot.modules.common.vo.Search;
import com.hqyj.springboot.modules.test.dao.StudentDao;
import com.hqyj.springboot.modules.test.entity.Student;
import com.hqyj.springboot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional
    public Result<Student> insertStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Insert success.");
    }

    @Override
    @Transactional
    public Result<Student> updateStudent(Student student) {
        studentDao.saveAndFlush(student);
        return new Result<>(Result.ResultStatus.SUCCESS.code,
                "Update success.");
    }

    @Override
    @Transactional
    public Result<Object> deleteStudentById(int id) {
       studentDao.deleteById(id);
       return new Result<>(Result.ResultStatus.SUCCESS.code,
               "Delete success.");
    }



    @Override
    public Student getStudentById(int id) {
//        return studentDao.getById(id);
        return studentDao.getStudentById(id);
    }

    @Override
    public Page getStudentsBySearch(Search search) {
        search.init();
        //设置排序规则
        Sort sort = Sort.by(
                search.getDirection().toLowerCase().equals("asc")
                ? Sort.Direction.ASC : Sort.Direction.DESC
                , search.getSort());

        //设置分页
        /**
         * PageRequest:它的页码是从0开始的
         */
        PageRequest pageable = PageRequest.of(
                search.getCurrentPage() - 1,
                search.getPageSize(),
                sort);

//        查询的关键字
        Student student = new Student();

        student.setStudentName(search.getKeyword());

        /**
         * withMatcher():设置匹配规则
         * withIgnorePaths():忽略某一个属性
         * matchingAny():or条件查询
         * matchingAll():and条件查询
         */
        ExampleMatcher matcher = ExampleMatcher.matchingAny()
                .withMatcher("studentName",
                        matcher1 -> matcher1.contains())
                .withIgnorePaths("id");

//        根据实体bean的属性以及设定的模板匹配器规则，到数据库查询
        Example<Student> example = Example.of(student, matcher);

       return studentDao.findAll(example,pageable);
    }

    @Override
    public List<Student> getStudentsByStudentName(String studentName) {
        return Optional.ofNullable(studentDao.findByStudentName(studentName))
                .orElse(Collections.emptyList());
    }
}
